/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.ricemine.services.identity;

import com.logiclander.ricemine.utils.LdapUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.kuali.rice.kim.bo.entity.dto.KimEntityVisaInfo;
import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextAdapter;

/**
 *
 * @author tcarroll
 */
public class LdapKimEntityVisaInfoMapper implements ContextMapper {

  private final Log log = LogFactory.getLog(getClass());
  private String entityIdAttributeName = null;
  private String visaEntryAttributeName = null;
  private String visaIdAttributeName = null;
  private String visaTypeKeyAttributeName = null;

//beg: methods required by ContextMapper interface
  public Object mapFromContext(Object ctx) {
    validateConfiguration();
    KimEntityVisaInfo kevi = new KimEntityVisaInfo();
    try {
      DirContextAdapter context = (DirContextAdapter) ctx;
      // potential to be special or synthetic, so do some special processing
      if (context.getStringAttribute(entityIdAttributeName).equalsIgnoreCase("dn")) {
        kevi.setEntityId(LdapUtilities.buildDn(context, true));
      } else if (context.getStringAttribute(entityIdAttributeName).equals("objectGUID")) {
        kevi.setEntityId(LdapUtilities.buildGuid(context));
      } else {
        kevi.setEntityId(context.getStringAttribute(entityIdAttributeName));
      }
      // currently, this assumes that an entity only has one visa (the one aligned
      // with this ldap record.  this is a decent assumption for now, but a configurable
      // option for either doing it this way or mapping multiple visas would be better
      // perhaps some organizations have multi-valued attributes that list visas

      // the id is a synthetic identifier for the instance of an entity visa.
      // this should be unique in the system, but there is no graceful way to do this
      // from an ldap mapping
      kevi.setId("1");//is there an ldap attribute for this?
      kevi.setVisaEntry(LdapUtilities.materialize(context.getStringAttribute(visaEntryAttributeName)));
      kevi.setVisaId(LdapUtilities.materialize(context.getStringAttribute(visaIdAttributeName)));
      kevi.setVisaTypeKey(LdapUtilities.materialize(context.getStringAttribute(visaTypeKeyAttributeName)));
    } catch (Throwable t) {
      log.error("Error in ContextMapper", t);
      String msg = "ContextMapper failed to create a LdapRecord from the specified Context: " + ctx;
      throw new RuntimeException(msg, t);
    }
    if (log.isDebugEnabled()) {
      log.debug("mapped KimEntityVisa: entityId=" + kevi.getEntityId());
    }
    return kevi;
  }

//end: methods required by ContextMapper interface
//beg: helper methods
  public String getEntityIdAttributeName() {
    return this.entityIdAttributeName;
  }

  public void setEntityIdAttributeName(String attrName) {
    this.entityIdAttributeName = attrName;
  }

  public String getVisaEntryAttributeName() {
    return this.visaEntryAttributeName;
  }

  public void setVisaEntryAttributeName(String attrName) {
    this.visaEntryAttributeName = attrName;
  }

  public String getVisaIdAttributeName() {
    return this.visaIdAttributeName;
  }

  public void setVisaIdAttributeName(String attrName) {
    this.visaIdAttributeName = attrName;
  }

  public String getVisaTypeKeyAttributeName() {
    return this.visaTypeKeyAttributeName;
  }

  public void setVisaTypeKeyAttributeName(String attrName) {
    this.visaTypeKeyAttributeName = attrName;
  }

  private void validateConfiguration() {
    if (entityIdAttributeName == null) {
      String msg = "The property 'entityIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (visaEntryAttributeName == null) {
      String msg = "The property 'visaEntryAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (visaIdAttributeName == null) {
      String msg = "The property 'visaIdAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
    if (visaTypeKeyAttributeName == null) {
      String msg = "The property 'visaTypeKeyAttributeName' must be set.";
      throw new IllegalStateException(msg);
    }
  }
//end: helper methods
}
